package com.example.slidingexample;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.example.slidingexample.loader.LazyAdapter;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends SherlockActivity {
	SlidingMenu menu;
	public static final String url = "";
	String web_data = "";
	String[] activity_id, title, price, imageurl, distance;
	private ListView list = null;
	Activity a;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ActionBar ab = getSupportActionBar();
		ab.setHomeButtonEnabled(true);
		ab.setDisplayHomeAsUpEnabled(true);
		menu = new SlidingMenu(this);
		menu.setMode(SlidingMenu.LEFT);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setMenu(R.layout.activity_menu);

		a = this;

		list = (ListView) findViewById(R.id.list);

		Runnable ViewOrder = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					web_data = Utility.getData(url);
					runOnUiThread(returnRes);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Thread thread = new Thread(ViewOrder);
		thread.start();
	}

	Runnable returnRes = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			// Log.v("tag", web_data);

			if (web_data != null) {

				JSONObject jo;
				try {
					jo = new JSONObject(web_data);
					JSONArray ja = jo.getJSONArray("activities");

					activity_id = new String[ja.length()];
					title = new String[ja.length()];
					price = new String[ja.length()];
					imageurl = new String[ja.length()];
					distance = new String[ja.length()];

					for (int i = 0; i < ja.length(); i++) {

						JSONObject jo1 = (JSONObject) ja.get(i);

						activity_id[i] = jo1.getString("activity_id");
						title[i] = jo1.getString("title");
						price[i] = jo1.getString("price");
						distance[i] = jo1.getString("distance");
						imageurl[i] = jo1.getJSONObject("header_image")
								.getString("medium");

						Log.v("tag", "" + imageurl[i]);

					}

					LazyAdapter adapter = new LazyAdapter(a,  title,distance,
							imageurl);
					list.setAdapter(adapter);

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		menu.toggle();
		return super.onOptionsItemSelected(item);
	}
}
